package com.cn.api.util; /**
 * @author: yihanyuan
 * @Description：
 * @Date: 2019/3/12 13:23
 */

import org.junit.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: demo
 * @description:
 * @author: yihanyuan
 * @create: 2019-03-12 13:23
 **/
public class Test4 {


    public void count1(List<BigDecimal> list, BigDecimal count) {
        BigDecimal low = list.stream().min((x1, x2) -> x1.compareTo(x2)).get();
        System.out.println("传入参数");
        list.stream().forEach(System.out::println);
        System.out.println("每个商品可购买的最多件数");
        List<BigDecimal[]> all = new ArrayList<>();
        //对价格排序
        List<BigDecimal> sort = list.stream().sorted().collect(Collectors.toList());
        sort.stream().forEach((x) -> {
            Integer max = (count.intValue() % x.intValue() == 0 ? count.intValue() / x.intValue() : count.intValue() / x.intValue() + 1);
            BigDecimal[] b = {BigDecimal.valueOf(max), x.multiply(BigDecimal.valueOf(max))};
            all.add(b);
            System.out.println("价格为  " + x + "  可以买  " + max + "  件,总价为" + x.multiply(BigDecimal.valueOf(max)));
        });
//        System.out.println("求出最小值" + low);
//        System.out.println("最多可添加最低价格商品" + (count.intValue() % low.intValue() == 0 ? count.intValue() / low.intValue() : count.intValue() / low.intValue() + 1) + "件");
        BigDecimal[] maxPrice = all.stream().max((x, y) -> x[1].compareTo(y[1])).get();
        //接受返回的集合
        List<BigDecimal[]> res = new ArrayList<>();
        int size = list.size();
        if (size == 1) {

        }
        if (size == 2) {

        }
        if (size == 3) {
            Count3(all, count, sort, maxPrice[1], res);
        }
        if (size == 4) {
            res = Count4(all, count, sort, maxPrice[1], res);
        }
        res.stream().sorted((x, y) -> x[size].compareTo(y[size])).limit(10).forEach(x -> {
            String s = "";
            System.out.println(Arrays.asList(x).toString());
            for (int i = 0; i < size; i++) {
                s += x[i] + "*" + sort.get(i);
                if (i < size - 1) {
                    s += "+";
                }
            }
            s += "=" + x[size];
            System.out.println(s);
        });

    }

    public List<BigDecimal[]> Count3(List<BigDecimal[]> all, BigDecimal count, List<BigDecimal> sort, BigDecimal max, List<BigDecimal[]> res) {

        for (int i = 0; i <= all.get(0)[0].intValue(); i++) {
            for (int j = 0; j <= all.get(1)[0].intValue(); j++) {
                for (int k = 0; k <= all.get(2)[0].intValue(); k++) {
                    BigDecimal result = BigDecimal.valueOf(i).multiply(sort.get(0)).add(BigDecimal.valueOf(j).multiply(sort.get(1))).add(BigDecimal.valueOf(k).multiply(sort.get(2)));
                    if (result.compareTo(count) == 1 && max.compareTo(result) == 1) {
                        BigDecimal[] big = {BigDecimal.valueOf(i), BigDecimal.valueOf(j),
                                BigDecimal.valueOf(k),
                                result};
                        res.add(big);
                    }
                }
            }
        }
        return res;
    }

    public List<BigDecimal[]> Count4(List<BigDecimal[]> all, BigDecimal count, List<BigDecimal> sort, BigDecimal max, List<BigDecimal[]> res) {
        for (int i = 0; i <= all.get(0)[0].intValue(); i++) {
            for (int j = 0; j <= all.get(1)[0].intValue(); j++) {
                for (int k = 0; k <= all.get(2)[0].intValue(); k++) {
                    for (int l = 0; l <= all.get(3)[0].intValue(); l++) {
                        BigDecimal result = BigDecimal.valueOf(i).multiply(sort.get(0))
                                .add(BigDecimal.valueOf(j).multiply(sort.get(1)))
                                .add(BigDecimal.valueOf(k).multiply(sort.get(2)))
                                .add(BigDecimal.valueOf(l).multiply(sort.get(3)));
                        if (result.compareTo(count) == 1 && max.compareTo(result) == 1) {
                            BigDecimal[] big = {BigDecimal.valueOf(i), BigDecimal.valueOf(j),
                                    BigDecimal.valueOf(k),
                                    BigDecimal.valueOf(l),
                                    result};
                            res.add(big);
                        }
                    }
                }
            }
        }
        return res;
    }

    @Test
    public void te() {
        BigDecimal[][] big = {
                {new BigDecimal("0"),
                        new BigDecimal("1"),
                        new BigDecimal("2")},
                {new BigDecimal("0"),
                        new BigDecimal("1"),
                        new BigDecimal("2"),
                        new BigDecimal("3"),
                        new BigDecimal("4")},
                {new BigDecimal("0"),
                        new BigDecimal("1"),
                        new BigDecimal("2"),
                        new BigDecimal("3"),
                        new BigDecimal("4")},
                {new BigDecimal("0"),
                        new BigDecimal("1"),
                        new BigDecimal("2"),
                        new BigDecimal("3"),
                        new BigDecimal("4"),
                        new BigDecimal("5")}};
//        int i = 1;
//        for (int i = 0; i < big.length; i++) {
//            for (int j = 0; j < big[i].length; j++) {
//                System.out.println("i=" + i + "---------j=" + j);
//            }
//        }

        String[][] oldSpecs = {{"a1", "a2"}, {"b1", "b2", "b3"}, {"c1", "c2"}};

        String[] newSpecs = {};
        int i = 0;

//        for (int jj = 0; jj < oldSpecs[i].length(); jj++) {
//            newSpecs.push(oldSpecs[i][jj]);
//        }
//        if (i < oldSpecs.length) {
//            newSpecs = duang(newSpecs, ++i);
//            console.log(newSpecs);
//        }
//
//        function duang (newSpecs, i){
//            var thisNN = [];
//            for (var jj = 0; jj < newSpecs.length; jj++) {
//                for (var kk = 0; kk < oldSpecs[i].length; kk++) {
//                    thisNN.push(newSpecs[jj] + oldSpecs[i][kk]);
//                }
//            }
//            newSpecs = thisNN;
//            if (++i < oldSpecs.length) {
//                newSpecs = duang(newSpecs, i);
//            }
//
//            return newSpecs;
//        }
    }

    public static String multiRound(List<List<BigDecimal>> dataList, String temp, int index) {
        if (index >= dataList.size()) {
            return "";
        }
        StringBuffer out = new StringBuffer();
        String tmp = "";
        //取出第一个list
        List<BigDecimal> data = dataList.get(index);
        //对第一个list进行遍历
        for (int i = 0; i < data.size(); i++) {
            tmp = data.get(i) + "/";
            if (index < dataList.size()) {
//取出第一个list中的参数进行拼接，进入下面的循环，取出第二个list，以此继续循环至最后一个list中的最后一个参数，然后再取出第二个参数，继续循环
                out.append(multiRound(dataList, temp + tmp, index + 1));
//                out.append(multiRound(dataList, temp + "  " + index, index + 1));
            }
            //判断是否已经完了一个回合，完了就加入到集合中
            if (index == dataList.size() - 1) {
                out.append(temp).append(tmp).append("\n");
            }
        }
        return out.toString();
    }

    @Test
    public void count() {
//        BigDecimal i1 = new BigDecimal("8");
//        BigDecimal i2 = new BigDecimal("4");
//        BigDecimal i3 = new BigDecimal("6");
//        //满减价格
//        BigDecimal count = new BigDecimal("21");
        BigDecimal i1 = new BigDecimal("20");
        BigDecimal i2 = new BigDecimal("48");
        BigDecimal i3 = new BigDecimal("29.9");
        BigDecimal i4 = new BigDecimal("25");
        //满减价格
        BigDecimal count = new BigDecimal("90");
        List<BigDecimal> list = new ArrayList<>();
        list.add(i1);
        list.add(i2);
        list.add(i3);
        list.add(i4);
        count1(list, count);
    }

    public void count(List<BigDecimal> list, BigDecimal count) {
        BigDecimal low = list.stream().min((x1, x2) -> x1.compareTo(x2)).get();
        System.out.println("传入参数");
        list.stream().forEach(System.out::println);
        System.out.println("每个商品可购买的最多件数");
        //计算每个商品最多可以买多少件划算，第一个参数保存最多多少件，第二个参数保存购买后的价格
        List<BigDecimal[]> all = new ArrayList<>();
        //对价格排序
        List<BigDecimal> sort = list.stream().sorted().collect(Collectors.toList());
        sort.stream().forEach((x) -> {
            Integer max = (count.intValue() % x.intValue() == 0 ? count.intValue() / x.intValue() : count.intValue() / x.intValue() + 1);
            BigDecimal[] b = {BigDecimal.valueOf(max), x.multiply(BigDecimal.valueOf(max))};
            all.add(b);
            System.out.println("价格为  " + x + "  可以买  " + max + "  件,总价为" + x.multiply(BigDecimal.valueOf(max)));
        });
        //取出话最多钱买的商品的那一个数组，之后凑单价格不能超过这个，超过及作废
        BigDecimal[] maxPrice = all.stream().max((x, y) -> x[1].compareTo(y[1])).get();
        List<List<BigDecimal>> lists = new ArrayList<>();
        all.stream().forEach(x -> {
            List<BigDecimal> l = new ArrayList<>();
            for (int i = 0; i <= x[0].intValue(); i++) {
                l.add(BigDecimal.valueOf(i));
            }
            lists.add(l);
        });
        lists.stream().forEach(System.out::println);
        List<String> result = Arrays.asList(multiRound(lists, "", 0).split("\n"));
        //计算最后的价格
        //接受返回的集合
        List<String[]> res = new ArrayList<>();
        List<String[]> list1 = countPrice(result, sort, count, maxPrice[1], res);
        list1.stream()
                .sorted((x, y) -> BigDecimal.valueOf(Double.valueOf(x[1])).compareTo(BigDecimal.valueOf(Double.valueOf(y[1]))))
                .limit(10)
                .map(s -> s[0])
                .forEach(System.out::println);
    }

    public List<String[]> countPrice(List<String> result, List<BigDecimal> sort, BigDecimal minPrice, BigDecimal maxPrice, List<String[]> res) {
        result.stream().forEach(s -> {
            String[] string = s.split("/");
            StringBuffer sb = new StringBuffer("");
            BigDecimal price = new BigDecimal("0");
            for (int i = 0; i < string.length; i++) {
                //价格就算表达式
                price = price.add(BigDecimal.valueOf(Long.valueOf(string[i])).multiply(sort.get(i)));
                sb.append(string[i]).append("*").append(sort.get(i));
                if (i < string.length - 1) {
                    sb.append("+");
                }
            }
            if (price.compareTo(minPrice) == 1 && maxPrice.compareTo(price) == 1) {
                sb.append("=").append(price);
                String[] ss = {sb.toString(), price.toString()};
                res.add(ss);
            }
        });
        return res;
    }
}
